/*
 * @: 
 * @version: 1.0
 * @Author: ziwei
 * @Date: 2021-10-19 00:04:07
 * @LastEditors: ziwei
 * @LastEditTime: 2021-11-01 17:37:44
 */
import {createStore,applyMiddleware} from '../redux'
import rootReducer from './reducers'
import promise from '../redux-promise'
import logger from '../redux-logger'
import thunk from '../redux-thunk'
//let store  = createStore(rootReducer)
//中间件 不管是什么功能 格式是固定的
//getSyaye=store.getState  dispatch=store.dispatch
// function logger({getState,dispatch}){
//     return function(next){//next调用下一个中间件 next=store,原始dispatch方法
//         return function(action){//这个就是改造后的dispatch
//             console.log('pre state',getState());
//             next(action) //oldDispatch
//             console.log('next state',getState());

//         }
//     }
// }

let store = applyMiddleware(thunk,promise,logger)(createStore)(rootReducer)
//let oldDispatch = store.dispatch;
//日志功能
/* store.dispatch = function(action){
    console.log('pre state',store.getState());
    oldDispatch(action)
    console.log('next state',store.getState());
    return action;
} */

/* 
中间件可以实现
1:打日志
2:实现异步操作 
*/
/* store.dispatch = function(action){
    setTimeout(() => {
        oldDispatch(action)
    }, 1000);
    return action
} */
//发送请求，异步请求数据
/* store.dispatch =function(action){
    fetch('./user.json').then(res=>res.json()).then(res=>{
        console.log(res);
        oldDispatch(action)
    })
} */

export default store